<?php

/**
 * This is the model class for table "offre".
 *
 * The followings are the available columns in table 'offre':
 * @property integer $id
 * @property integer $idseliste
 * @property integer $iddemande
 * @property string $date_propose
 * @property string $date_offre
 * @property integer $nb
 * @property integer $valeur
 * @property string $status
 * @property string $commentaire
 *
 * The followings are the available model relations:
 * @property Echange[] $echanges
 * @property Demande $iddemande0
 * @property Seliste $idseliste0
 */
class Offre extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'offre';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('idseliste, iddemande, date_propose, status', 'required'),
			array('idseliste, iddemande, nb, valeur', 'numerical', 'integerOnly'=>true),
			array('status', 'length', 'max'=>1),
			array('date_propose','checkdatepropose'),
			array('date_offre, commentaire', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, idseliste, iddemande, date_propose, date_offre, nb, valeur, status, commentaire', 'safe', 'on'=>'search'),
		);
	}
	
	public function checkdatepropose($attribute,$params)
	{
		if ($this->date_propose<date('Y-m-d')) $this->addError('date_propose','La date saisie doit être supérieure ou égale à la date du jour.');
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'echanges' => array(self::HAS_MANY, 'Echange', 'idoffre'),
			'iddemande0' => array(self::BELONGS_TO, 'Demande', 'iddemande'),
			'idseliste0' => array(self::BELONGS_TO, 'Seliste', 'idseliste'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Numéro',
			'idseliste' => 'Séliste',
			'iddemande' => 'Demande',
			'date_propose' => 'Date proposée',
			'date_offre' => 'Date de l\'offre',
			'nb' => 'Nombre d\'unités',
			'valeur' => 'Valeur',
			'status' => 'Status',
			'commentaire' => 'Commentaire',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('idseliste',$this->idseliste);
		$criteria->compare('iddemande',$this->iddemande);
		$criteria->compare('date_propose',$this->date_propose,true);
		$criteria->compare('date_offre',$this->date_offre,true);
		$criteria->compare('nb',$this->nb);
		$criteria->compare('valeur',$this->valeur);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('commentaire',$this->commentaire,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
				'pagination'=>array(
						'pageSize'=>40,
				),
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Offre the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	/* retourne le status */
	public function getStatus() {
		switch ($this->status) {
			case 0 :	return "Annulée par l'offrant";
						break;
			case 1 :	return "En cours";
						break;
			case 2 :	return "Acceptée par le demandeur";
						break;
			case 3 :	return "Conclue";
						break;
			case 4 :	return "Conclue par un autre offrant";
						break;
			case 5 :	return "Annulée par le demandeur";
						break;
			case 6 :	return "Litige";
						break;
			case 7 :	return "Litige résolu";
						break;
						
		}
	}
	
	public function getNbOffres() {
		return Offre::model()->countByAttributes(array(
				'idseliste'=> Yii::app()->user->id,
				'status'=>1,
		));
	}
}
